package com.dashu.authority.servie.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dashu.authority.common.Constant;
import com.dashu.authority.entity.Role;
import com.dashu.authority.entity.UserAndRole;
import com.dashu.authority.exception.AuthorityException;
import com.dashu.authority.repository.RoleMapper;
import com.dashu.authority.repository.UserAndRoleMapper;
import com.dashu.authority.servie.UserAndRoleService;
import com.dashu.authority.util.AuthorityServiceUtil;

@Service
public class UserAndRoleServiceImpl extends BaseServiceImpl<UserAndRole>implements UserAndRoleService {

	@Autowired
	private UserAndRoleMapper userAndRoleMapper;
	
	@Autowired
	private RoleMapper roleMapper;

	@Override
	public List<UserAndRole> queryAllByUserId(Integer userId) throws AuthorityException {
		AuthorityServiceUtil.checkParam("用户ID", userId);
		Map<Object, Object> params = new HashMap<Object, Object>();
		params.put("userId", userId);
		return userAndRoleMapper.queryAllByExample(params);
	}

	@Override
	public List<UserAndRole> queryAllByRoleId(Integer roleId) throws AuthorityException{
		AuthorityServiceUtil.checkParam("角色ID", roleId);
		Map<Object, Object> params = new HashMap<Object, Object>();
		params.put("roleId", roleId);
		return userAndRoleMapper.queryAllByExample(params);
	}

	@Override
	public Map<String, Object> saveAll(Integer userId, Integer[] roleIds) throws AuthorityException {
		AuthorityServiceUtil.checkParam("用户ID", userId);
		userAndRoleMapper.deleteByUserid(userId);
		if (roleIds==null||roleIds.length==0) {
			return AuthorityServiceUtil.OperaSuccessMap(Constant.OPERATION_SAVE);
		}
		List<UserAndRole> list = new ArrayList<UserAndRole>();
		for (Integer role : roleIds) {
			UserAndRole a = new UserAndRole();
			a.setUserId(userId);
			a.setRoleId(role);
			list.add(a);
		}
		Map<Object, Object> params = new HashMap<Object,Object>();
		params.put("lists", list);
		int ret=userAndRoleMapper.insertByBatch(params);
		if (ret > 0) {
			return AuthorityServiceUtil.OperaSuccessMap(Constant.OPERATION_SAVE);
		} else {
			return AuthorityServiceUtil.OperaFailMap(Constant.OPERATION_SAVE);
		}
	}

	/**
	 * 
	 * @ClassName: UserAndRoleServiceImpl.java
	 * @Description: 根据用户id查询该用户的所有角色以及上级角色 
	 * @author andy.wang
	 * @date 2017年9月26日 上午11:30:13 
	 * @param @param userId
	 * @param @return
	 * @param @throws AuthorityException
	 * @return List<UserAndRole>
	 *
	 */
	@Override
	public Set<Integer> querySuperiorRoleByUserId(Integer userId) throws AuthorityException {
		AuthorityServiceUtil.checkParam("用户ID", userId);
		Map<Object, Object> params = new HashMap<Object, Object>();
		params.put("userId", userId);
		List<UserAndRole> list = userAndRoleMapper.queryAllByExample(params);
		if (list==null||list.size()==0) {
			return null;
		}
		Set<Integer> set = new HashSet<Integer>();
		for (UserAndRole userAndRole : list) {
			List<Role> roles = roleMapper.findAllSuperiorByRoleId(userAndRole.getRoleId());
			if (roles!=null&&roles.size()>0) {
				for (int i = 0; i < roles.size(); i++) {
					set.add(roles.get(i).getId());
				}
			}
		}
		return set;
	}
}
